home *** CD-ROM | disk | FTP | other *** search
/ Java Primer Plus / Java Primer Plus (Waite Group Proess)(1996).iso / java_Win / demo / SpreadSheet / Cell.class (.txt) next >
Encoding:
Java Class File  |  1995-10-12  |  5.4 KB  |  269 lines

  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3.  
  4. class Cell {
  5.    public static final int VALUE = 0;
  6.    public static final int LABEL = 1;
  7.    public static final int URL = 2;
  8.    public static final int FORMULA = 3;
  9.    Node parseRoot;
  10.    boolean needRedisplay;
  11.    boolean selected = false;
  12.    boolean transientValue = false;
  13.    public int type;
  14.    String valueString = "";
  15.    String printString = "v";
  16.    float value;
  17.    Color bgColor;
  18.    Color fgColor;
  19.    Color highlightColor;
  20.    int width;
  21.    int height;
  22.    SpreadSheet app;
  23.    CellUpdater updaterThread;
  24.    boolean paused = false;
  25.  
  26.    public Cell(SpreadSheet app, Color bgColor, Color fgColor, Color highlightColor, int width, int height) {
  27.       this.app = app;
  28.       this.bgColor = bgColor;
  29.       this.fgColor = fgColor;
  30.       this.highlightColor = highlightColor;
  31.       this.width = width;
  32.       this.height = height;
  33.       this.needRedisplay = true;
  34.    }
  35.  
  36.    public void setRawValue(float f) {
  37.       this.valueString = Float.toString(f);
  38.       this.value = f;
  39.    }
  40.  
  41.    public void setValue(float f) {
  42.       this.setRawValue(f);
  43.       this.printString = "v" + this.valueString;
  44.       this.type = 0;
  45.       this.paused = false;
  46.       this.app.recalculate();
  47.       this.needRedisplay = true;
  48.    }
  49.  
  50.    public void setTransientValue(float f) {
  51.       this.transientValue = true;
  52.       this.value = f;
  53.       this.needRedisplay = true;
  54.       this.app.recalculate();
  55.    }
  56.  
  57.    public void setUnparsedValue(String s) {
  58.       switch (s.charAt(0)) {
  59.          case 'v':
  60.             this.setValue(0, s.substring(1));
  61.             return;
  62.          case 'u':
  63.             this.setValue(2, s.substring(1));
  64.             return;
  65.          case 'l':
  66.             this.setValue(1, s.substring(1));
  67.             return;
  68.          case 'f':
  69.             this.setValue(3, s.substring(1));
  70.             return;
  71.          default:
  72.       }
  73.    }
  74.  
  75.    public String parseFormula(String formula, Node node) {
  76.       formula.length();
  77.       if (formula == null) {
  78.          return null;
  79.       } else {
  80.          String subformula = this.parseValue(formula, node);
  81.          if (subformula != null && subformula.length() != 0) {
  82.             if (subformula == formula) {
  83.                return formula;
  84.             } else {
  85.                char op;
  86.                switch (op = subformula.charAt(0)) {
  87.                   case '/':
  88.                   case '-':
  89.                   case '+':
  90.                   case '*':
  91.                      String restFormula = subformula.substring(1);
  92.                      Node right;
  93.                      subformula = this.parseValue(restFormula, right = new Node());
  94.                      if (subformula != restFormula) {
  95.                         Node left = new Node(node);
  96.                         node.left = left;
  97.                         node.right = right;
  98.                         node.op = op;
  99.                         node.type = 0;
  100.                         return subformula;
  101.                      }
  102.  
  103.                      return formula;
  104.                   case ')':
  105.                      return subformula;
  106.                   case '\u0000':
  107.                      return null;
  108.                   default:
  109.                      return formula;
  110.                }
  111.             }
  112.          } else {
  113.             return null;
  114.          }
  115.       }
  116.    }
  117.  
  118.    public String parseValue(String formula, Node node) {
  119.       char c = formula.charAt(0);
  120.       String restFormula = formula;
  121.       if (c == '(') {
  122.          restFormula = formula.substring(1);
  123.          String subformula = this.parseFormula(restFormula, node);
  124.          if (subformula == null || subformula.length() == restFormula.length()) {
  125.             return formula;
  126.          }
  127.  
  128.          if (subformula.charAt(0) != ')') {
  129.             return formula;
  130.          }
  131.  
  132.          restFormula = subformula;
  133.       } else {
  134.          if (c >= '0' && c <= '9') {
  135.             float var6;
  136.             try {
  137.                var6 = Float.valueOf(formula);
  138.             } catch (NumberFormatException var10) {
  139.                return formula;
  140.             }
  141.  
  142.             int i;
  143.             for(i = 0; i < formula.length(); ++i) {
  144.                c = formula.charAt(i);
  145.                if ((c < '0' || c > '9') && c != '.') {
  146.                   break;
  147.                }
  148.             }
  149.  
  150.             node.type = 1;
  151.             node.value = var6;
  152.             restFormula = formula.substring(i);
  153.             return restFormula;
  154.          }
  155.  
  156.          if (c >= 'A' && c <= 'Z') {
  157.             int column = c - 65;
  158.             restFormula = formula.substring(1);
  159.             int row = Float.valueOf(restFormula).intValue();
  160.  
  161.             int i;
  162.             for(i = 0; i < restFormula.length(); ++i) {
  163.                c = restFormula.charAt(i);
  164.                if (c < '0' || c > '9') {
  165.                   break;
  166.                }
  167.             }
  168.  
  169.             node.row = row - 1;
  170.             node.column = column;
  171.             node.type = 2;
  172.             if (i == restFormula.length()) {
  173.                restFormula = null;
  174.             } else {
  175.                restFormula = restFormula.substring(i);
  176.                if (restFormula.charAt(0) == 0) {
  177.                   return null;
  178.                }
  179.             }
  180.          }
  181.       }
  182.  
  183.       return restFormula;
  184.    }
  185.  
  186.    public void setValue(int type, String s) {
  187.       this.paused = false;
  188.       if (this.type == 2) {
  189.          this.updaterThread.stop();
  190.          this.updaterThread = null;
  191.       }
  192.  
  193.       this.valueString = new String(s);
  194.       this.type = type;
  195.       this.needRedisplay = true;
  196.       switch (type) {
  197.          case 0:
  198.             this.setValue(Float.valueOf(s));
  199.             break;
  200.          case 1:
  201.             this.printString = "l" + this.valueString;
  202.             break;
  203.          case 2:
  204.             this.printString = "u" + this.valueString;
  205.             this.updaterThread = new CellUpdater(this);
  206.             this.updaterThread.start();
  207.             break;
  208.          case 3:
  209.             this.parseFormula(this.valueString, this.parseRoot = new Node());
  210.             this.printString = "f" + this.valueString;
  211.       }
  212.  
  213.       this.app.recalculate();
  214.    }
  215.  
  216.    public String getValueString() {
  217.       return this.valueString;
  218.    }
  219.  
  220.    public String getPrintString() {
  221.       return this.printString;
  222.    }
  223.  
  224.    public void select() {
  225.       this.selected = true;
  226.       this.paused = true;
  227.    }
  228.  
  229.    public void deselect() {
  230.       this.selected = false;
  231.       this.paused = false;
  232.       this.needRedisplay = true;
  233.       this.app.repaint();
  234.    }
  235.  
  236.    public void paint(Graphics g, int x, int y) {
  237.       if (this.selected) {
  238.          g.setColor(this.highlightColor);
  239.       } else {
  240.          g.setColor(this.bgColor);
  241.       }
  242.  
  243.       g.fillRect(x, y, this.width - 1, this.height);
  244.       if (this.valueString != null) {
  245.          switch (this.type) {
  246.             case 0:
  247.             case 1:
  248.                g.setColor(this.fgColor);
  249.                break;
  250.             case 2:
  251.                g.setColor(Color.blue);
  252.                break;
  253.             case 3:
  254.                g.setColor(Color.red);
  255.          }
  256.  
  257.          if (this.transientValue) {
  258.             g.drawString("" + this.value, x, y + this.height / 2 + 5);
  259.          } else if (this.valueString.length() > 14) {
  260.             g.drawString(this.valueString.substring(0, 14), x, y + this.height / 2 + 5);
  261.          } else {
  262.             g.drawString(this.valueString, x, y + this.height / 2 + 5);
  263.          }
  264.       }
  265.  
  266.       this.needRedisplay = false;
  267.    }
  268. }
  269.